package com.ftdigital.primemovers_operators.fragment;


import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.ftdigital.primemovers_operators.MainActivity;
import com.ftdigital.primemovers_operators.MyApplication;
import com.ftdigital.primemovers_operators.R;
import com.ftdigital.primemovers_operators.config.GlobalData;
import com.ftdigital.primemovers_operators.config.RequestAddr;
import com.ftdigital.primemovers_operators.model.BaseModel;
import com.ftdigital.primemovers_operators.util.MethedUtil;
import com.ftdigital.primemovers_operators.util.http.AjaxCallBack;
import com.google.gson.JsonSyntaxException;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 *添加庫存
 */
@EFragment(R.layout.fragment_add_inventroy)
public class AddInventroyFragment extends BaseFragment {

    @ViewById(R.id.back_btn)
    ImageButton backBtn;

    @ViewById(R.id.toptitle_title)
    TextView topTitle;

    @ViewById(R.id.oil_type_content)
    EditText oilType;
    @ViewById(R.id.oil_density_content)
    EditText oilDensity;
    @ViewById(R.id.apply_amount)
    EditText applyAmount;
    @ViewById(R.id.actual_amount)
    EditText actualAmount;
    @ViewById(R.id.remaining_amount)
    EditText remainingAmount;

    @AfterViews
    @Override
    void initView() {
        backBtn.setVisibility(View.VISIBLE);
        topTitle.setText(getResources().getString(R.string.title_add_inventory));
    }

    @Click({R.id.back_btn, R.id.submit})
    @Override
    void Click(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                MainActivity.getInstance().getSupportFragmentManager().popBackStack();
                break;
            case R.id.submit:
                if(checkValidate()) {
                    submitData();
                }
                break;
        }
    }

    private boolean checkValidate() {
        if (oilType.getText().toString().equals("")||
                oilDensity.getText().toString().equals("")||
                applyAmount.getText().toString().equals("")||
                actualAmount.getText().toString().equals("")||
                remainingAmount.getText().toString().equals("")) {
            Toast.makeText(getActivity(), "请填写完整", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (MethedUtil.isCheckedSpeChar(getActivity(), applyAmount.getText().toString())||
                MethedUtil.isCheckedSpeChar(getActivity(), actualAmount.getText().toString()) ||
                MethedUtil.isCheckedSpeChar(getActivity(), remainingAmount.getText().toString())) {
            return false;
        }
        return true;
    }
    private void submitData() {
        try {
            requestParams = "addamount="+applyAmount.getText().toString().trim()
            +"&density="+oilDensity.getText().toString().trim()
                    +"&gas_type="+ URLEncoder.encode(oilType.getText().toString().trim(), "utf-8")
                    +"&gasstation_id="+ MyApplication.userInfo.getData().getGasstation_id()
                    +"&realaddamount="+actualAmount.getText().toString().trim()
                    +"&sumamount="+remainingAmount.getText().toString().trim();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        requestUrl = RequestAddr.addDayInventory + requestParams;
        xtHttp.get(requestUrl, new AjaxCallBack<Object>() {
            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }

            @Override
            public void onSuccess(Object object) {
                super.onSuccess(object);
                dealData(object);
            }
        });
    }

    private void dealData(Object object) {
        try {
            baseModel = gson.fromJson(object.toString(), BaseModel.class);
        } catch (JsonSyntaxException e) {
            return;
        }
        Toast.makeText(getActivity(),baseModel.getMsg(), Toast.LENGTH_SHORT).show();
        MainActivity.getInstance().getSupportFragmentManager().popBackStack();
    }


}
